DO...LOOP Statement ---------------------------------------------------------------------------- Action Repeats a block of statements while a condition is true or until a condition becomes true. Syntax 1 DO -{ WHILE | UNTIL} condition- - statementblock- - EXIT DO- - statementblock- LOOP Syntax 2 DO - statementblock- - EXIT DO- - statementblock- LOOP -{ WHILE | UNTIL} condition- Remarks The argument condition is a numeric expression that BASIC evaluates as true (nonzero) or false (zero). The program lines between the DO and LOOP statements will be repeated as long as condition is true. You can use a DO... LOOP statement instead of a WHILE... WEND statement. The DO... LOOP is more versatile because it can test for a condition at the beginning or at the end of a loop. Examples The following examples show how placement of the condition affects the number of times the block of statements is executed. ' Test at the beginning of the loop. Because I is not less than 10, ' the body of the loop (the statement block) is never executed. I = 10 PRINT "Example 1.". PRINT PRINT "Value of I at beginning of loop is "; I DO WHILE I < 10 I = I + 1 LOOP PRINT "Value of I at end of loop is "; I ' Test at the end of the loop, so the statement block executes ' at least once. I = 10 PRINT . PRINT . PRINT "Example 2.". PRINT DO PRINT "Value of I at beginning of loop is "; I I = I + 1 LOOP WHILE I < 10 PRINT "Value of I at end of loop is "; I The following sort program tests at the end of the loop because the entire array must be examined at least once to see if it is in order. The program illustrates testing at the end of a loop. CONST NOEXCH = -1' Set up a value to indicate no exchanges. DIM Exes(12) DIM I AS INTEGER ' Load the array and mix it up. FOR I = 1 TO 12 STEP 2 Exes(I) = 13 - I Exes(I + 1) = 0 + I NEXT I Limit = 12 PRINT PRINT "This is the list of numbers to sort in ascending order." PRINT FOR I = 1 TO 12 PRINT USING " ### "; Exes(I); NEXT I PRINT ' In the following DO...LOOP, INKEY$ is tested at the bottom of ' the loop. When the user presses a key, INKEY$ is no longer a ' null string and the loop terminates, continuing program execution. PRINT . PRINT "Press any key to continue." DO LOOP WHILE INKEY$ = "" DO Exchange = NOEXCH FOR I = 1 TO Limit - 1 ' Make one pass over the array. IF Exes(I) > Exes(I + 1) THEN SWAP Exes(I), Exes(I + 1) ' Exchange array elements. Exchange = I ' Record location of most END IF ' recent exchange. NEXT I Limit = Exchange ' Sort on next pass only to where ' last exchange was done. LOOP UNTIL Exchange = NOEXCH ' Sort until no elements are ' exchanged. PRINT . PRINT "Sorting is completed. This is the sorted list.". PRINT FOR I = 1 TO 12 PRINT USING " ### "; Exes(I); NEXT I END